package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.exception.WebException;
import com.cl.short_video_java.pojo.VideoLikeCollect;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.pojo.VideoVideo;
import com.cl.short_video_java.service.VideoLikeCollectService;
import com.cl.short_video_java.mapper.VideoLikeCollectMapper;
import com.cl.short_video_java.service.VideoUserService;
import com.cl.short_video_java.service.VideoVideoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author object
 * @description 针对表【video_like_collect】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class VideoLikeCollectServiceImpl extends ServiceImpl<VideoLikeCollectMapper, VideoLikeCollect>
		implements VideoLikeCollectService {

	private VideoUserService videoUserService;
	private VideoVideoService videoVideoService;

	VideoLikeCollectServiceImpl(VideoUserService videoUserService, VideoVideoService videoVideoService) {
		this.videoUserService = videoUserService;
		this.videoVideoService = videoVideoService;
	}

	/**
	 * 获取用户点赞-收藏视频的数量
	 *
	 * @param type    1-收藏，2-点赞
	 * @param request
	 * @param current
	 * @return
	 */
	@Override
	public Page<VideoLikeCollect> likeOrCollect(Integer type, HttpServletRequest request, Integer current) {

		VideoUser user = videoUserService.getCurrentUser(request, Const.GRADE_USER);

		Page<VideoLikeCollect> page = this.page(new Page<>(current, 20), new QueryWrapper<VideoLikeCollect>()
				.eq("type", type == 1 ? 1 : 0)
				.eq("user_id", user.getId()).eq("is_delete", 0)
				.orderByDesc("create_time")
		);

		for (VideoLikeCollect videoLikeCollect : page.getRecords()) {
			VideoVideo video = videoVideoService.getOne(new QueryWrapper<VideoVideo>()
					.eq("id", videoLikeCollect.getVideoId())
					.eq("is_delete", 0)
					.eq("status", 1)
			);
			videoLikeCollect.setVideo(video);
		}
		return page;
	}

	/**
	 * 取消点赞-收藏
	 *
	 * @param type
	 * @param videoId
	 * @param request
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void cancel(Integer type, Integer videoId, HttpServletRequest request) {
		VideoUser user = videoUserService.getCurrentUser(request, Const.GRADE_USER);
		VideoLikeCollect one = this.getOne(new QueryWrapper<VideoLikeCollect>()
				.eq("video_id", videoId)
				.eq("user_id", user.getId())
				.eq("type", type == 3 ? 1 : 0)
		);
		VideoVideo video = videoVideoService.getOne(new QueryWrapper<VideoVideo>()
				.eq("id", videoId)
				.eq("is_delete", 0));
		if (one == null) {
			throw new WebException("当前数据不存在");
		}

		one.setType(2);
		this.saveOrUpdate(one);
		if (video != null) {
			if (type == 3) {
				video.setCollection(video.getCollection() - 1);
			} else {
				video.setThumbUp(video.getThumbUp() - 1);
			}
			videoVideoService.updateById(video);
		}

	}
}




